package com.sto.springmvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sto.springmvc.model.system.StoLog;
import com.sto.springmvc.service.system.StoLogService;

@Controller
public class StoLogController extends BaseController {

	@Autowired
	private StoLogService stoLogService;

	@RequestMapping("/")
	public ModelAndView index(ModelAndView modelAndView) {
		modelAndView.setViewName("list");
		List<StoLog> stoLogList = stoLogService.selectByMap(null);
		modelAndView.addObject("stoLogList", stoLogList);
		return modelAndView;
	}

	@RequestMapping("/search")
	public ModelAndView search(StoLog stoLog, ModelAndView modelAndView) {
		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("systemName", stoLog.getSystemName());
		modelAndView.addObject("systemName", stoLog.getSystemName());
		columnMap.put("logLevel", stoLog.getLogLevel());
		modelAndView.addObject("logLevel", stoLog.getLogLevel());
		columnMap.put("logContent", stoLog.getLogContent());
		modelAndView.addObject("logContent", stoLog.getLogContent());
		columnMap.put("starttime", stoLog.getStarttime());
		modelAndView.addObject("starttime", stoLog.getStarttime());
		columnMap.put("endtime", stoLog.getEndtime());
		modelAndView.addObject("endtime", stoLog.getEndtime());
		List<StoLog> stoLogList = stoLogService.selectByMap(columnMap);
		modelAndView.addObject("stoLogList", stoLogList);
		modelAndView.setViewName("list");
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("/detail/{id}")
	public Object detail(@PathVariable String id) {
		if (null != id && "" != id) {
			return stoLogService.selectById(id) != null ? renderSuccess(stoLogService.selectById(id))
					: renderError("数据不存在");
		} else {
			return renderError("主键不存在");
		}
	}

	@RequestMapping("/preSave")
	public ModelAndView preSave(ModelAndView modelAndView, @RequestParam(value = "id", required = false) Long id) {
		modelAndView.setViewName("save");
		if (id != null) {
			modelAndView.addObject("stoLog", stoLogService.selectById(id));
		}
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("save")
	public Object save(StoLog stoLog) {
		if (stoLog.getId() == null) {
			return stoLogService.insert(stoLog) ? renderSuccess("添加成功") : renderError("添加失败");
		} else {
			return stoLogService.updateById(stoLog) ? renderSuccess("修改成功") : renderError("修改失败");
		}
	}

	@ResponseBody
	@RequestMapping("/delete")
	public Object delete(@RequestParam(value = "id", required = false) Long id) {
		return stoLogService.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
	}
}
